package com.gs.common.util

import android.Manifest
import android.app.Activity
import android.content.Context
import android.os.Build
import com.amap.api.location.AMapLocationClient
import com.amap.api.location.AMapLocationClientOption
import com.amap.api.location.AMapLocationListener
import pub.devrel.easypermissions.AfterPermissionGranted
import pub.devrel.easypermissions.EasyPermissions

class LocationUtil {

    private lateinit var mLocationClient: AMapLocationClient // 声明AMapLocationClient类对象
    private lateinit var mLocationOption: AMapLocationClientOption // 声明AMapLocationClientOption类对象


    fun initLocation(activity: Activity, aMapLocationListener: AMapLocationListener): AMapLocationClient{
        // 初始化定位
        try {
            mLocationClient = AMapLocationClient(activity.applicationContext)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        // 设置定位回调监听
        mLocationClient.setLocationListener(aMapLocationListener)
        // 初始化AMapLocationClientOption对象
        mLocationOption = AMapLocationClientOption()
        // 设置定位模式为AMapLocationMode.Hight_Accuracy, 高精度模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy)
        // 获取最近3s内精度最高的一次定位结果
        // 设置setOnceLocationLatest(b: Boolean)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(b: Boolean)接口也会被设置为true， 反之不会，默认为false
        mLocationOption.setOnceLocationLatest(false)
        // 设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true)
        // 设置定位请求超时时间，单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒
        mLocationOption.setHttpTimeOut(200000)
        // 关闭缓存机制，高精度定位会产生缓存
        mLocationOption.setLocationCacheEnable(false)
        // 给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption)

        return mLocationClient
    }

    fun startLocation(activity: Activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android6.0及以上先获取权限再启动定位
            requestLocationRequiredPermission(activity)
        } else {
            // 以下直接启动定位
            mLocationClient.startLocation()
        }
    }

    @AfterPermissionGranted(REQUEST_LOCATION_PERMISSIONS)
    private fun requestLocationRequiredPermission(activity: Activity) {
        if (EasyPermissions.hasPermissions(
                activity,
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        ) {
            // true 有权限 开启定位
//            ToastUtil.showShortToast(this, "已获得权限，可以定位啦！")
            if (mLocationClient.isStarted) mLocationClient.stopLocation()
            mLocationClient.startLocation()
        } else {
            // false 无权限 rationale：根本原因
            EasyPermissions.requestPermissions(
                activity, "需要权限", REQUEST_LOCATION_PERMISSIONS,
                Manifest.permission.ACCESS_LOCATION_EXTRA_COMMANDS,
                Manifest.permission.ACCESS_COARSE_LOCATION,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE
            )
        }
    }

    companion object {
        private const val REQUEST_LOCATION_PERMISSIONS = 100 //用于标识一个权限请求，标识用于回调，数字任意
    }
}